package com.cloudm.technician.extensions

import android.content.Context
import android.net.Uri
import android.provider.MediaStore


/**
 * 作者: wesley
 * 时间: 2017/12/26
 * 描述: uri转路径
 */

fun Uri.parseUriToPath(context: Context): String? {
    val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
    val cursor = context.contentResolver.query(this, filePathColumn, null, null, null)
    return when (cursor) {
        null -> ""
        else -> {
            try {
                cursor.moveToFirst()
                val columnIndex = cursor.getColumnIndex(filePathColumn[0])
                val picturePath = cursor.getString(columnIndex)
                cursor.close()
                picturePath
            } catch (e: Exception) {
                logger(" parseUriToPath 异常信息 = ${e.message}")
                ""
            }
        }
    }
}